//
//  ButtonUtil.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/12.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "ButtonUtil.h"
#import "Masonry.h"
#import "ImageUtil.h"

@implementation ButtonUtil

#pragma mark Public
+(void)loadItemButtonWithButton:(UIButton *)btn
               withImageAddress:(NSString *)imageAddress
                      withTitle:(NSString *)title
            withBackGroundColor:(UIColor *)backgroundColor
       andButtonEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style{
    
    UIImageView *itemPicture = [UIImageView new];
    itemPicture.tag = ItemPictureTag;
    itemPicture.contentMode = UIViewContentModeScaleAspectFit;
    UIImage *itemImage = [UIImage imageNamed:imageAddress];
    if (itemImage == nil) {
        [itemPicture setIconWithKey:imageAddress withPlaceHolderString:@""];
    }else{
       [itemPicture setImage:itemImage];
    }
    
    UILabel *itemLabel = [UILabel new];
    itemLabel.tag = ItemLabelTag;
    itemLabel.backgroundColor = backgroundColor;
    itemLabel.textColor = [UIColor whiteColor];
    itemLabel.textAlignment = NSTextAlignmentCenter;
    itemLabel.font = TextFont12;
    [itemLabel setText:title];
    
    [btn addSubview:itemPicture];
    [btn addSubview:itemLabel];
    
    switch (style) {
        case ButtonEdgeInsetsStyleTop:
        {
            [itemPicture mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(btn);
                make.top.equalTo(btn).offset(5);
                make.width.equalTo(btn).multipliedBy(0.7);
                make.height.equalTo(itemPicture.mas_width);
            }];
            [itemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(btn);
                make.width.equalTo(btn);
                make.top.equalTo(itemPicture.mas_bottom).offset(5);
                make.bottom.lessThanOrEqualTo(btn);
                make.height.mas_greaterThanOrEqualTo(20).priorityLow();
            }];
        }
            break;
        case ButtonEdgeInsetsStyleLeft:
        {
            
        }
            break;
        case ButtonEdgeInsetsStyleBottom:
        {
            
        }
            break;
        case ButtonEdgeInsetsStyleRight:
        {
            [itemPicture mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(btn);
                make.height.equalTo(btn).multipliedBy(0.5);
                make.width.equalTo(itemPicture.mas_height).priorityLow();
                make.width.lessThanOrEqualTo(itemPicture.mas_height);
                make.right.equalTo(btn).offset(-5);
            }];
            [itemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(btn);
                make.right.equalTo(itemPicture.mas_left).offset(-5);
                make.left.equalTo(btn).offset(10);
            }];
        }
            break;
        default:
            break;
    }
}


+(void)loadBooleanSelectButtonGroupWithTitles:(NSArray *)titles withSelectedColor:(UIColor *)selectBackgroundColor withUnSelectedColor:(UIColor *)unSelectBackgroundColor withSelectTextColor:(UIColor *)selectTextColor withUnSelectTextColor:(UIColor *)unSelectTextColor andBackView:(UIView *)backView{
    UIButton *lastButton;
    for (int i=0; i<titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i+3;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:unSelectTextColor forState:UIControlStateNormal];
        [button setTitleColor:selectTextColor forState:UIControlStateSelected];
        [button setBackgroundImage:[ImageUtil imageWithColor:unSelectBackgroundColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[ImageUtil imageWithColor:selectBackgroundColor] forState:UIControlStateSelected];
        [backView addSubview:button];
        
        if (i==0) {
            
            [button setSelected:YES];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.equalTo(backView);
            }];
        }else if(i != (titles.count-1)){
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastButton.mas_right);
                make.top.bottom.equalTo(lastButton);
                make.width.equalTo(lastButton);
            }];
        }else{
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastButton.mas_right);
                make.top.bottom.equalTo(lastButton);
                make.width.equalTo(lastButton);
                make.right.equalTo(backView);
            }];
        }
        lastButton = button;
    }
}

@end
